package org.jzs.mybaseapp.common.utils;

import android.Manifest;
import android.app.Activity;
import android.support.v4.app.Fragment;

import com.yanzhenjie.permission.AndPermission;

/**
 * Created by jzs on 2017/3/17.
 * QQ:355392668
 * Edit in zhuoxun
 * <p>
 * 权限请求工具类
 * 注意：请重写一个Activity和Fragment的请求方法，在Fragment中使用getActivity()是无效的
 */

public class PermissionUtils {
    /**
     * 联系人权限
     */
    public static final int REQUEST_CODE_PERMISSION_CONTACTS = 100;
    /**
     * 手机状态权限(拨打电话等)
     */
    public static final int REQUEST_CODE_PERMISSION_PHONE = 101;
    /**
     * 日历权限
     */
    public static final int REQUEST_CODE_PERMISSION_CALENDAR = 102;
    /**
     * 相机权限
     */
    public static final int REQUEST_CODE_PERMISSION_CAMERA = 103;
    /**
     * 体感器权限(如运动)
     */
    public static final int REQUEST_CODE_PERMISSION_SENSORS = 104;
    /**
     * 定位权限
     */
    public static final int REQUEST_CODE_PERMISSION_LOCATION = 105;
    /**
     * 读写存储权限
     */
    public static final int REQUEST_CODE_PERMISSION_STORAGE = 106;
    /**
     * 麦克风权限
     */
    public static final int REQUEST_CODE_PERMISSION_MICROPHONE = 107;
    /**
     * 短信权限
     */
    public static final int REQUEST_CODE_PERMISSION_SMS = 108;
    /**
     * 相机和存储权限
     */
    public static final int REQUEST_CODE_PERMISSION_CAMERA_STORAGE = 110;
    /**
     * 自定义权限
     */
    public static final int REQUEST_CODE_PERMISSION_CUSTOM = 109;


    static PermissionUtils mInstance = null;

    public static PermissionUtils getInstance() {
        if (mInstance == null) {
            mInstance = new PermissionUtils();
        }
        return mInstance;
    }

    /**
     * 自定义权限
     * 根据需求一次请求多个权限
     */
    public void requestCustom(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_CUSTOM)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .send();
    }

    /**
     * 自定义权限
     * 根据需求一次请求多个权限
     */
    public void requestCustom(Fragment f) {
        AndPermission.with(f)
                .requestCode(REQUEST_CODE_PERMISSION_CUSTOM)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .send();
    }

    /**
     * 相机和存储权限
     */
    public void requestCameraAndStorage(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_CAMERA_STORAGE)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .send();
    }

    /**
     * 请求相机权限
     */
    public void requestCamera(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_CAMERA)
                .permission(Manifest.permission.CAMERA)
                .send();
    }

    /**
     * 请求电话权限
     */
    public void requestPhone(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_PHONE)
                .permission(Manifest.permission.CALL_PHONE)
                .send();
    }

    /**
     * 请求定位权限
     */
    public void requestLocation(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_LOCATION)
                .permission(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                .send();
    }

    /**
     * 请求定位权限
     */
    public void requestLocation(Fragment f) {
        AndPermission.with(f)
                .requestCode(REQUEST_CODE_PERMISSION_LOCATION)
                .permission(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                .send();
    }

    /**
     * 请求读写权限
     */
    public void requestStorage(Activity a) {
        AndPermission.with(a)
                .requestCode(REQUEST_CODE_PERMISSION_STORAGE)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .send();
    }

    /**
     * 以下方法需写在调用权限的页面
     * 根据requestCode来写相应的申请权限成功和失败的代码
     *
     */
//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
//    }
//    private PermissionListener listener = new PermissionListener() {
//        @Override
//        public void onSucceed(int requestCode, List<String> grantedPermissions) {
//            // 权限申请成功回调。
//            if (requestCode == REQUEST_CODE_PERMISSION_CAMERA) {
//                // TODO 相应代码。
//            }
//        }
//        @Override
//        public void onFailed(int requestCode, List<String> deniedPermissions) {
//            // 权限申请失败回调。
//            switch (requestCode) {
//                case REQUEST_CODE_PERMISSION_CAMERA:
//                    //弹出提示框
//                    AndPermission.defaultSettingDialog(this, REQUEST_CODE_PERMISSION_CAMERA).show();
//                    break;
//
//                default:
//
//                    break;
//            }
//        }
//    };

}
